package com.reguang.system.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.reguang.pojo.SysRoleDO;
import com.reguang.service.SysRoleService;

import lombok.extern.slf4j.Slf4j;

import com.reguang.result.Result;

/**
 * 
 * <p>
 * Title: Role
 * </p>
 * <p>
 * Description: 角色
 * </p>
 * 
 * @author weibiao
 * @date 2020-01-15 16:49:54
 */
@Controller
@RequestMapping("/sys/role")
@Slf4j
public class SysRoleController {

	public String prefix = "system/role";

	@Autowired
	private SysRoleService sysRoleService;

	@GetMapping()
	String Role() {
		return prefix + "/role";
	}

	@GetMapping("/list")
	@ResponseBody
	PageInfo<SysRoleDO> list(@RequestParam Map<String, Object> params) {
		// 返回列表数据
		return sysRoleService.list(params);
	}

	@GetMapping("/add")
	String add() {
		return prefix + "/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id, Model model) {
		SysRoleDO role = sysRoleService.queryByPrimaryKey(id);
		model.addAttribute("role", role);
		return prefix + "/edit";
	}

	@ResponseBody
	@PostMapping("/save")
	public Result save(SysRoleDO role ) {
		if (sysRoleService.save(role) > 0) {
			return Result.success();
		}
		return Result.failure();
	}

	@ResponseBody
	@RequestMapping("/update")
	public Result update(SysRoleDO role) {
		if (sysRoleService.update(role) > 0) {
			return Result.success();
		}
		return Result.failure();
	}

	@PostMapping("/remove")
	@ResponseBody
	public Result remove(Integer id) {
		if (sysRoleService.deleteById(id) > 0) {
			return Result.success();
		}
		return Result.failure();
	}
}
